import CryptoJs from "crypto-js";
const KEY = CryptoJs.enc.Utf8.parse('abcdefgh12345678');
const IV = CryptoJs.enc.Utf8.parse('12345678abcdefgh');
export function encrypto(word, keyStr, ivStr) {
  let key = KEY;
  let iv = IV;
  if (keyStr) {
    key = CryptoJs.enc.Utf8.parse(keyStr);
    iv = CryptoJs.enc.Utf8.parse(ivStr);
  }
  let srcs = CryptoJs.enc.Utf8.parse(word);
  var encrypted = CryptoJs.AES.encrypt(srcs, key, {
    iv,
    mode: CryptoJs.mode.CBC,
    padding: CryptoJs.pad.Pkcs7
  });
  return CryptoJs.enc.Base64.stringify(encrypted.ciphertext);
}

export function decrypto(word, keyStr, ivStr) {
  let key = KEY;
  let iv = IV;
  if (keyStr) {
    key = CryptoJs.enc.Utf8.parse(keyStr);
    iv = CryptoJs.enc.Utf8.parse(ivStr);
  }
  var decrypted = CryptoJs.AES.decrypt(word, key, {
    iv,
    mode: CryptoJs.mode.CBC,
    padding: CryptoJs.pad.Pkcs7
  });
  return decrypted.toString(CryptoJs.enc.Utf8);
}

export function entrypParams(params) {
  const entrypParams = {};
  for (let key in params) {
    if (params.hasOwnProperty(key)) {
      entrypParams[key] = encrypto(params[key])
    }
  }
  return entrypParams;
}

window.decrypto = function (ciphertext) {
  return decrypto(window.decodeURIComponent(ciphertext))
}